package com.viroyal.monitorlib.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;

import com.suntiago.baseui.utils.log.Slog;
import com.viroyal.monitorlib.network.ApiCallBack;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Time:2019/12/24
 * Author:bichao
 * description:
 */
public class CaptureUtils {

    public void getViewBp(ApiCallBack callBack) {
        View v = getRootView();
        Slog.d("CaptureUtils", "CaptureWidth=" + v.getWidth() + "CaptureHeight=" + v.getHeight());
        if (null == v) {
            callBack.onError();
        }
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache();
        if (Build.VERSION.SDK_INT >= 19) {
            v.measure(View.MeasureSpec.makeMeasureSpec(v.getWidth(), View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(v.getHeight(), View.MeasureSpec.EXACTLY));
            v.layout((int) v.getX(), (int) v.getY(), (int) v.getX() + v.getMeasuredWidth(), (int) v.getY() + v.getMeasuredHeight());
        } else {
            v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
        }
        Bitmap b = Bitmap.createBitmap(v.getDrawingCache(), 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
        Slog.d("CaptureUtils Bitmap=",b.getByteCount()+"");
        v.setDrawingCacheEnabled(false);
        v.destroyDrawingCache();

        saveImageToGallery(b, callBack);
    }

    public void saveImageToGallery(Bitmap bmp,ApiCallBack callBack) {
        //生成路径
        String root = Environment.getExternalStorageDirectory() + "/capture/";
        String dirName = "monitor";
        File appDir = new File(root , dirName);
        if (!appDir.exists()) {
            appDir.mkdirs();
        }
        Slog.d("CaptureUtils  bitmapPath=",appDir.getPath());
        //文件名为时间
        long timeStamp = System.currentTimeMillis();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        String sd = sdf.format(new Date(timeStamp));
        String fileName = sd + "-" + timeStamp + ".png";

        //获取文件
        File file = new File(appDir, fileName);
        FileOutputStream fos = null;
        Slog.d("CaptureUtils  FilePath=",file.getPath());
        try {
            fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Slog.e("CaptureUtils FileNotFoundException",e.getMessage()+"");
        } catch (IOException e) {
            e.printStackTrace();
            Slog.e("CaptureUtils IOException1",e.getMessage()+"");
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                Slog.e("CaptureUtils IOException2",e.getMessage()+"");
            }
        }
        callBack.onResult(file.getPath());
    }

    //获取根view
    private View getRootView() {
        Activity activity = MyActivityManager.getInstance().getCurrentActivity();
        if (activity != null) {
            return ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
        }
        return null;
    }
}
